/**
 * http.js
 * 封装axios，
 * 调用方法:
 * http.get('/api/enquiry/web/query',{id:1}).then((res)=>{你的操作})
 * http.post('/api/enquiry/web/update',{id:1}).then((res)=>{你的操作})
 * http.postFormData('/api/enquiry/web/update',{id:1,file:file}).then((res)=>{你的操作})
 */
import axios from 'axios'
import { router } from '../router'

axios.defaults.baseURL = '/apis';

// 请求拦截器
axios.interceptors.request.use(
    config => {
        // 每次发送请求之前判断vuex中是否存在token
        // 如果存在，则统一在http请求的header都加上token，这样后台根据token判断你的登录情况
        // 即使本地存在token，也有可能token是过期的，所以在响应拦截器中要对返回状态进行判断
        const token = localStorage.getItem('X-SEER-AUTH')
        token && (config.headers.Authorization = token);
        return config;
    },
    error => {
        return Promise.error(error);
    })

// 添加响应拦截器
axios.interceptors.response.use(
    response => {
        // 对响应数据做点什么
        return response;//返回相应的数据
    }, error => {
        if (error.response.status && (error.response.status === 401 || error.response.status === 403)) {
            localStorage.removeItem('X-SEER-AUTH')
            router.push('/')
        }
        return Promise.reject(error);
    });

/**
 * 封装get方法
 * @param url
 * @param params
 * @returns {Promise}
 */

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}

export function getFile(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params,
            responseType: 'blob'
        })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}

export function getForList(url, params, name) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params,
            paramsSerializer: function (params) {
                const keys = params.list.map(_ => name + `=${_}`).join('&');
                return `${keys}`;
            }
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

export function formPost(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios({
            headers: {
                'Content-Type': 'multipart/form-data'
            },
            method: 'post',
            url: url,
            data: data
        }).then(response => {
            resolve(response.data);
        }, err => {
            reject(err)
        })
    })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function put(url, data) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

export function deleteApi(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {
            params: params
        })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}

export function deleteForList(url, params, _paramsSerializer) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {
            params: params,
            paramsSerializer: _paramsSerializer
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}